What is promise-timeout?
The `promise-timeout` npm package provides utilities to add timeout functionality to promises. It allows you to set a time limit on a promise, after which it will be rejected if not resolved. This is useful for handling cases where you want to ensure that certain asynchronous operations do not take longer than a specified duration.
What are promise-timeout's main functionalities?
Timeout a Promise
This feature allows you to set a timeout on a promise. If the promise does not resolve within the specified time (1000ms in this case), it will be rejected with a timeout error.
const { timeout } = require('promise-timeout');
const myPromise = new Promise((resolve) => {
setTimeout(() => resolve('Success!'), 2000);
});
const timeoutPromise = timeout(myPromise, 1000);
timeoutPromise
.then((result) => console.log(result))
.catch((err) => console.error('Promise timed out:', err));
Timeout with Custom Error
This feature allows you to set a timeout on a promise with a custom error message. If the promise does not resolve within the specified time (1000ms in this case), it will be rejected with the custom timeout error.
const { timeout, TimeoutError } = require('promise-timeout');
const myPromise = new Promise((resolve) => {
setTimeout(() => resolve('Success!'), 2000);
});
const timeoutPromise = timeout(myPromise, 1000, new TimeoutError('Custom timeout error'));
timeoutPromise
.then((result) => console.log(result))
.catch((err) => console.error('Promise timed out:', err.message));
Other packages similar to promise-timeout
p-timeout
The `p-timeout` package provides similar functionality to `promise-timeout` by allowing you to set a timeout on a promise. It also offers additional features such as custom error messages and fallback values. Compared to `promise-timeout`, `p-timeout` has a more extensive API and better integration with other promise-based utilities.
promise-time
The `promise-time` package offers timeout functionality for promises, similar to `promise-timeout`. It allows you to specify a timeout duration and will reject the promise if it does not resolve within that time. `promise-time` is simpler and more lightweight compared to `promise-timeout`, making it a good choice for projects with minimal dependencies.
promise-timeout
A super-simple way to put a timeout on promise resolution.
It assumes you already have either platform support for promises (Node 0.12 or
greater), or you have a polyfill (see es6-promise).
Installation
$ npm install promise-timeout
Usage
timeout(promise, timeoutMillis)
Rejects a promise with a TimeoutError
if it does not settle within the
specified timeout. Parameters:
promise: Promise
- Promise to monitor.timeoutMillis: number
- Number of milliseconds to wait on settling.
TimeoutError
Exception indicating that the timeout expired.
Examples
ES2015:
import { timeout, TimeoutError } from 'promise-timeout';
let somePromise = goDoSomething();
timeout(somePromise, 1000)
.then((thing) => console.log('I did a thing!'))
.catch((err) => {
if (err instanceof TimeoutError) {
console.error('Timeout :-(');
}
});
ES5:
'use strict';
var pt = require('promise-timeout');
var somePromise = goDoSomething();
pt.timeout(somePromise, 1000)
.then(function (thing) {
console.log('I did a thing!');
}).catch(function (err) {
if (err instanceof pt.TimeoutError) {
console.error('Timeout :-(');
}
});